<template>
  <div class="login-container">
    <img class="login-logo" src="../../assets/images/login_logo.png" alt="">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">账号登录</h3>
      </div>

      <el-form-item prop="account">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="account"
          v-model="loginForm.account"
          placeholder="Username"
          name="account"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="Password"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          />
        </span>
      </el-form-item>

      <el-button
        :loading="loading"
        type="primary"
        class="login-btn"
        style=""
        @click.native.prevent="handleLogin"
      >登 录</el-button>

      <div class="tips">
        <div class="text">记住密码</div>
        <el-switch
          v-model="remember"
          active-color="#13ce66"
          inactive-color="#fff"
        />
        <!-- <span style="margin-right: 20px">account: admin</span>
        <span> password: any</span> -->
      </div>
    </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'

export default {
  name: 'Login',
  data () {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        account: localStorage.getItem('account') || '',
        password: localStorage.getItem('password') || ''
      },
      loginRules: {
        account: [{ required: true, trigger: 'blur' }],
        password: [{ required: true, trigger: 'blur' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      remember: localStorage.getItem('remember') == 'true'
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd () {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin () {
      localStorage.setItem('account', this.loginForm.account)
      localStorage.setItem('remember', this.remember)
      if (this.remember) {
        localStorage.setItem('password', this.loginForm.password)
      } else {
        localStorage.removeItem('password')
      }
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              // this.$router.push({ path: this.redirect || '/' })
              this.$router.push({ path: '/' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

.login-btn {
  width: 100%;
  margin-bottom: 30px;
  height: 60px;
  font-size: 18px;
  background-color: #2860c3;
  border: none;
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    background: #fff;

    input {
      background: #fff;
      border: 0px;
      // -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      // color: $light_gray;
      color: #999999;
      height: 47px;
      // caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  // background-color: $bg;
  background: url(../../assets/images/login_bg.png) no-repeat center/cover;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 630px;
    height: 470px;
    max-width: 100%;
    padding: 0px 35px 0;
    margin: 0 auto;
    margin-top: 315px;
    background-color: #fff;
    overflow: hidden;
    border-radius: 20px;
  }

  .tips {
    display: flex;
    align-items: center;
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    ::v-deep.el-switch__core:after {
      background-color: #ccc;
    }
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
    .text {
      color: #666666;
      margin-right: 10px;
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 24px;
      color: #2860c3;
      // color: $light_gray;
      margin: 40px auto 40px auto;
      // text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}

::v-deep.login-container .el-form-item {
  background-color: #fff !important;
  border-bottom: 1px solid #e7e7e7;
}
.el-switch {
  ::v-deep.el-switch__core {
    border-color: #ccc !important;
  }
}

.login-logo {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  margin-top: 122px;
  width: 468px;
  height: 131px;
}
</style>
